package main

import (
	"encoding/json"
	"fmt"
)

type Movie struct {
	Title  string   `json:title`
	Year   int      `json:year`
	Price  int      `json:price`
	Actors []string `json:actors`
}

func main() {
	movie := Movie{"喜剧之王", 2000, 10, []string{"周星驰", "张柏芝"}}
	// 可将一个结构体转换成一个json格式
	// 结构体  -->  json
	jsonstr, err := json.Marshal(movie)
	if err != nil {
		fmt.Println("json maeshal error ", err)
		return
	}

	fmt.Printf("jsonstr = %s\n", jsonstr)

	// josn  -->  结构体
	myMovie := Movie{}
	err2 := json.Unmarshal(jsonstr, &myMovie)
	if err2 != nil {
		fmt.Println("josn unmarshal error ", err2)
		return
	}

	fmt.Printf("myMovie = %v\n", myMovie)

}
